//
//  DateSelectView.swift
//  AirlineReservation
//
//  Created by Kevin on 2020/1/22.
//  Copyright © 2020 Kevin. All rights reserved.
//

import UIKit

class DateSelectView: UIView {
    
    // 星期
    private lazy var weekDays = [UIButton]()
    // 月日
    private lazy var monthDays = [UIButton]()
    // 金额
    private lazy var firstMoneys = [UIButton]()
    
    // 个数
    private let num = 12
    // 竖线
    private lazy var lines = [UIView]()
    
    // 今天
    private lazy var todayBackground = UIView()
    private lazy var todayText = UILabel(title: "今天")
    private lazy var todayPic = UIImageView(image: UIImage(named: "bookingCalendar_20x20"))
    
    // 提示
    private lazy var tip = UIView()
    private lazy var tipText = UILabel(title: "今天",color: UIColor.orange,size: 13)
    private lazy var tipPic = UIImageView(image: UIImage(named: "tips_12x12"))
    
    // 背景
    private lazy var scrollView: UIScrollView = {
        let scroll = UIScrollView()
        scroll.backgroundColor = COLOR_LIGHT_WHITE
        return scroll
    }()
    
    // MARK: 设置条数
    public var ticketNumber:Int? {
        didSet {
            tipText.text = "共\(ticketNumber!)条相关信息(价格不含税费及燃油费)"
        }
    }
    // MARK: 设置金额
    public var firstmoneys:[Int]? {
        didSet {
            setNeedsLayout()
        }
    }
    
    // MARK: 日期切到某天
    public func selectDate(index:Int) -> Void {
        dateSelect(btn: monthDays[index])
    }
    
    // 回调
    public var complete: ((_ item:String) -> Void)?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        init_views()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        guard let moneys = firstmoneys else {
            return
        }
        if moneys.count > num {
            return
        }
        // 宽分成6个
        let w = frame.width / 6
        let h = ROW_HEIGHT / 3 - MARGIN5
        // 设置scrollview
        init_scroll_set(w: w, moneys: moneys)
        // 日期布局
        init_date_layout(w: w, h: h, moneys: moneys)
        // 今天布局
        init_today_layout(w:w)
        // 提示布局
        init_tip_count_layout()
    }
}

// MARK: 逻辑部分
extension DateSelectView {
    
    // MARK: 日期选中
    @objc func clickItem(btn:UIButton) -> Void {
        // 日期选中
        dateSelect(btn: btn)
        // 回调数据
        if let callback = complete {
            let date = "\(AirLineUtil.shareInstance.getCurrYear())-" + monthDays[btn.tag].currentTitle!
            callback(date)
        }
    }
}

// MARK: 代码不动部分
extension DateSelectView {
    
    // MARK: 设置Scrollview
    private func init_scroll_set(w:CGFloat,moneys:[Int]) -> Void {
        scrollView.showsHorizontalScrollIndicator = false
        scrollView.frame.size = CGSize(width: frame.width, height: ROW_HEIGHT)
        scrollView.contentSize = CGSize(width: w * CGFloat(moneys.count + 1), height: ROW_HEIGHT)
    }
    
    // MARK: 日期布局
    private func init_date_layout(w:CGFloat,h:CGFloat,moneys:[Int]) -> Void {
        for i in 0..<moneys.count {
            let dataX = CGFloat(i) * w
            // 月日
            let monthDay = monthDays[i]
            monthDay.frame = CGRect(x: dataX, y: 3, width: w - MARGIN10, height: h + MARGIN5)
            // 星期
            let weekDay = weekDays[i]
            weekDay.frame = CGRect(x: dataX, y: monthDay.frame.maxY - MARGIN5 , width: w - MARGIN10, height: h + MARGIN10)
            // 竖线
            let line = lines[i]
            line.frame = CGRect(x: CGFloat(i + 1) * w - MARGIN10, y: monthDay.frame.maxY - MARGIN5 , width: 1, height: h + MARGIN10)
            // 金额
            let money = firstMoneys[i]
            money.setTitle("¥\(moneys[i])", for: .normal)
            money.frame = CGRect(x: dataX, y: weekDay.frame.maxY - MARGIN5, width: w - MARGIN10, height: h + MARGIN5)
            // 监听点击事件
            money.addTarget(self, action: #selector(clickItem(btn:)), for: .touchUpInside)
            weekDay.addTarget(self, action: #selector(clickItem(btn:)), for: .touchUpInside)
            monthDay.addTarget(self, action: #selector(clickItem(btn:)), for: .touchUpInside)
        }
    }
    
    // MARK: 今天布局
    private func init_today_layout(w:CGFloat) -> Void {
        // 今天背景
        todayBackground.frame = CGRect(x: frame.width - w, y: 0, width: w, height: ROW_HEIGHT)
        todayBackground.backgroundColor = COLOR_LIGHT_WHITE2
        // 今天图片
        todayPic.frame = CGRect(x: (w - MARGIN20) * 0.5, y: (ROW_HEIGHT - MARGIN30) * 0.5 , width: MARGIN20, height: MARGIN20)
        todayBackground.addSubview(todayPic)
        // 今天文本
        todayText.font = UIFont.systemFont(ofSize: 12)
        let todayWidth = todayText.text!.boundingRect(w: w, h: MARGIN, fontSize: 12).width * 0.5
        todayText.frame = CGRect(x: w / 2 - todayWidth, y: todayPic.frame.maxY + MARGIN5, width: w, height: MARGIN)
        todayBackground.addSubview(todayText)
        // 今天添加点击事件
        todayBackground.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(swithToFirstDate)))
        addSubview(todayBackground)
    }
    
    // MARK: 提示布局
    private func init_tip_count_layout() -> Void {
        guard let number = ticketNumber else {
            return
        }
        // 提示
        tip.frame = CGRect(x: 0, y: scrollView.frame.maxY, width: frame.width, height: MARGIN40)
        tip.backgroundColor = Palette.colorFromRGB(255, green: 251, blue: 237)
        // 提示图片
        tipPic.frame = CGRect(x: MARGIN20, y: 12.5, width: MARGIN15, height: MARGIN15)
        tip.addSubview(tipPic)
        // 提示文本
        tipText.text = "共\(number)条相关信息(价格不含税费及燃油费)"
        tipText.frame = CGRect(x: tipPic.frame.maxX + MARGIN5, y: 12.5, width: frame.width - tipPic.frame.maxX, height: MARGIN15)
        tip.addSubview(tipText)
        addSubview(tip)
    }
    
    // MARK: 日期切到今天
    @objc func swithToFirstDate() -> Void {
        switchToDateByIndex(index: 0)
    }
    
    // MARK: 日期切到某天
    public func switchToDateByIndex(index:Int) -> Void {
        clickItem(btn: monthDays[index])
        // 动画放慢滑动
        UIView.animate(withDuration: 0.5) {
            self.scrollView.contentOffset = CGPoint(x: 0, y: 0)
        }
    }
    
    // MARK: 日期选中
    private func dateSelect(btn:UIButton) -> Void {
        guard let moneys = firstmoneys else {
            return
        }
        for i in 0..<moneys.count {
            modifyBtnStatus(btn:weekDays[i],backroundColor: UIColor.clear,titleColor: UIColor.gray)
            modifyBtnStatus(btn:monthDays[i],backroundColor: UIColor.clear,titleColor: COLOR_NICE)
            modifyBtnStatus(btn:firstMoneys[i],backroundColor: UIColor.clear,titleColor: COLOR_NICE)
        }
        modifyBtnStatus(btn:weekDays[btn.tag])
        modifyBtnStatus(btn:monthDays[btn.tag])
        modifyBtnStatus(btn:firstMoneys[btn.tag])
    }
    
    // 修改背景颜色
    private func modifyBtnStatus(btn:UIButton,backroundColor:UIColor = COLOR_RED,titleColor:UIColor = UIColor.white) -> Void {
        btn.backgroundColor = backroundColor
        btn.setTitleColor(titleColor, for: .normal)
    }
}

// MARK: - 初始化View
extension DateSelectView {
    
    private func init_views() -> Void {
        // 星期
        let weeks = AirLineUtil.shareInstance.getDateForCount(count: num)
        // 月份
        let months = AirLineUtil.shareInstance.getDateForCount(count: num,month: true)
        for i in 0..<num {
            // 竖线
            let line = UIView()
            line.backgroundColor = COLOR_LINE
            lines.append(line)
            // 星期
            let weekDay = UIButton()
            weekDay.tag = i
            weekDay.setTitle(weeks[i], for: .normal)
            weekDay.setTitleColor(UIColor.gray, for: .normal)
            weekDay.titleLabel?.font = UIFont.systemFont(ofSize: 11)
            weekDays.append(weekDay)
            // 月日
            let monthDay = UIButton()
            monthDay.tag = i
            monthDay.layer.cornerRadius = 5
            monthDay.setTitle(months[i], for: .normal)
            monthDay.setTitleColor(COLOR_NICE, for: .normal)
            monthDay.titleLabel?.font = UIFont.systemFont(ofSize: 12)
            monthDays.append(monthDay)
            // 金额
            let firstMoney = UIButton()
            firstMoney.tag = i
            firstMoney.layer.cornerRadius = 5
            firstMoney.setTitleColor(COLOR_NICE, for: .normal)
            firstMoney.titleLabel?.font = UIFont.systemFont(ofSize: 12)
            firstMoneys.append(firstMoney)
            // 添加到父控件
            scrollView.addSubview(line)
            scrollView.addSubview(weekDay)
            scrollView.addSubview(monthDay)
            scrollView.addSubview(firstMoney)
            addSubview(scrollView)
        }
    }
}
